<?php
namespace app\common\model;

class CompanyStaffManage extends \app\common\model\BaseModel
{
    protected $resultSetType = 'collection';

    private static function setFieldValue($value){
        return [
            'com_id'=>$value['company_id'],
            'uid'=>$value['uid'],
            'age'=>date('Y') - intval($value['birthday']),
            'name'=>$value['fullname'],
            'education'=>model(self::class)->map_education[$value['education']]??'',
            'experience'=>$value['enter_job_time'] == 0 ? '应届生' : format_date($value['enter_job_time']),
            'gender'=>$value['sex']==1?'男':'女',
            'postname'=>$value['jobname'],
            'mobile'=>$value['mobile'],
            'mailbox'=>$value['email'],
            'job_nature'=>'全职',
            'entry_status'=>'正式员工',
            'entry_time'=>date('Y-m-d'),
            'contract_duration'=> 1,
            'coms_id'=> $value['coms_id'],
            'coms_d_id'=> $value['coms_d_id'],
        ];
    }
    public static function add($data){
        if((!$company_id = $data['company_id']??0) || (!$resume_id = $data['resume_id']??0)){
            return false;
        }
        $resume = Resume::where('id',$resume_id)->field('id,uid,fullname,sex,birthday,education,enter_job_time')->find();
        if((!$uid = $resume['uid']??0) || (self::where('uid',$uid)->count() > 0)){
            return false;
        }
        $request['result'] = $resume->toArray();
        $request['join'] = [
            'job_apply' => ['model' => JobApply::class,
                'field' => 'resume_id,jobid,jobname',
                'key' => 'resume_id',
                'foreign_key' => 'id'],
            'contact' => ['model' => ResumeContact::class,
                'field' => 'rid,mobile,email',
                'key' => 'rid',
                'foreign_key' => 'id']
        ];
        $request['result'] = leftJoinData($request,1);
        $request['join'] = [
            'department' => ['model' => Job::class,
                'field' => 'id,coms_id,coms_d_id',
                'key' => 'id',
                'foreign_key' => 'jobid']
        ];
        $resume = leftJoinData($request,1);
        $insert = self::setFieldValue($resume+['company_id'=>$company_id]);
        self::insert($insert);
        return true;
    }

    public static function addBatch($data){
        if((!$company_id = $data['company_id']??0) || (!$resume_ids = $data['resume_ids']??0)){
            return false;
        }
        $resume = Resume::whereIn('id',$resume_ids)->field('id,uid,fullname,sex,birthday,education,enter_job_time')->select();
        $exists = self::whereIn('uid',array_column($resume,'uid'))->column('uid');

        $request['result'] = $resume;
        $request['join'] = [
            'job_apply' => ['model' => JobApply::class,
                'field' => 'resume_id,jobid,jobname',
                'key' => 'resume_id',
                'foreign_key' => 'id'],
            'contact' => ['model' => ResumeContact::class,
                'field' => 'rid,mobile,email',
                'key' => 'rid',
                'foreign_key' => 'id']
        ];
        $request['result'] = leftJoinData($request);
        $request['join'] = [
            'department' => ['model' => Job::class,
                'field' => 'id,coms_id,coms_d_id',
                'key' => 'id',
                'foreign_key' => 'jobid']
            ];
        $resume = leftJoinData($request);

        $insert = [];
        foreach ($resume as $value){
            if(in_array($value['uid'],$exists)){
                continue;
            }
            $insert[] = self::setFieldValue($value->toArray() + ['company_id'=>$company_id]);
        }
        $insert && self::insertAll($insert);
        return true;
    }
}
